package com.wondersgroup.fjzlpt.org.service.impl.jgqy.lshgl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.quick.framework.exception.ServiceException;
import com.quick.framework.util.LoggerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.lshgl.LshglDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.lshgl.LshglLsDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgry.YljgryDAO;
import com.wondersgroup.fjzlpt.org.service.inter.jgqy.lshgl.LshglService;


@Service
public class LshglServiceImpl implements LshglService{

	@Autowired
	private LshglDAO lshglDAO;
	
	@Autowired
	private LshglLsDAO lshglLsDAO;
	
	@Autowired
	private YljgryDAO yljgryDAO;
	
	/**
	 * 保存
	 * @param map
	 * @return
	 */
	public JSONObject save(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		String method = map.get("method");
		if("add".equals(method)){
			insert(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 新增
	 * @param map
	 * @param ro
	 */
	private void insert(Map map, ResultObject ro){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Object CLSJ = map.get("CLSJ");
			if(CLSJ != null && !StringUtils.isEmpty(CLSJ.toString())){
				map.put("CLSJ", sdf.parse(CLSJ.toString()));
			}
			
			String LSHGLID = UUID.randomUUID().toString().toUpperCase();
			map.put("LSHGLID", LSHGLID);
			lshglDAO.insert(map);
			/**创建关联表插入的多条记录*/
			String lsids = map.get("LSIDS").toString();
			String[] ids = lsids.split(",");
			List<Map> list = new ArrayList<Map>();
			for (String str : ids) {
				Map  ma = new HashMap();
				ma.put("GLID", UUID.randomUUID().toString().toUpperCase());
				ma.put("LSHGLID", LSHGLID);
				ma.put("LSID", str);
				list.add(ma);
			}
			lshglLsDAO.insertBatch(list);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000521","新建理事会失败！");
		}
	}
	
	
	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	public JSONObject queryPage(Map<String, String> map) {
		return lshglDAO.queryPage(map);
	}

	/**
	 * 查询医疗机构人员信息
	 * @param map
	 * @return
	 */
	public JSONObject queryYljgrylb(Map<String, String> map) {
		return yljgryDAO.queryPage(map);
	}

	/**
	 * 查询理事姓名
	 * @param map
	 * @return
	 */
	public String queryLs(Map<String, String> map) {
		JSONArray ja = lshglLsDAO.query(map);
		Iterator it = ja.iterator();
		String lsxms = "";
		while(it.hasNext()){
			JSONObject json = JSONObject.parseObject(it.next().toString());
			lsxms += json.getString("LSXM")+",";
		}
		return lsxms.substring(0, lsxms.lastIndexOf(","));
	}

	
}
